# 修改记录：
#      1. 2024-08-30： 增加调试工具
#      2. 2024-09-07： 滑块自动绑定设置的 `HSV` 分量值

from ImageProcess import ImageProcessor
import cv2
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter.messagebox import showinfo

h1, h2, s1, s2, v1, v2 = [1, 2, 3, 4, 5, 6]
basePath = 'D:\\tmp\\test_imgs\\5'
def create_main_window():
   return cv2.namedWindow('main_win')

processor = ImageProcessor()
processor.debug = True
create_main_window()
mask_win = cv2.namedWindow('mask_win')

##########
def call_h1(v):
    tmp = processor.lower_blue
    tmp[0] = int(v)
    processor.lower_blue = tmp
    processor.process_x()
    processor.showTarget()

def call_h2(v):
    tmp = processor.upper_blue
    tmp[0] = int(v)
    processor.upper_blue = tmp
    processor.process_x()
    processor.showTarget()

########## S
def call_s1(v):
    tmp = processor.lower_blue
    tmp[1] = int(v)
    processor.lower_blue = tmp
    processor.process_x()
    processor.showTarget()

def call_s2(v):
    tmp = processor.upper_blue
    tmp[1] = int(v)
    processor.upper_blue = tmp
    processor.process_x()
    processor.showTarget()

########## V
def call_v1(v):
    tmp = processor.lower_blue
    tmp[2] = int(v)
    processor.lower_blue = tmp
    processor.process_x()
    processor.showTarget()

def call_v2(v):
    tmp = processor.upper_blue
    tmp[2] = int(v)
    processor.upper_blue = tmp
    processor.process_x()
    processor.showTarget()

root = tk.Tk()
root.title('Process Debug')
root.resizable(False, False)
root.geometry('600x300')

def select_files():
    filetypes = (
        ('All files', '*.*'),
        ('jpg files', '*.jpg'),
        ('png files', '*.png'),
    )

    filename = fd.askopenfilename(
        title='Open file',
        initialdir=basePath,
        filetypes=filetypes)

    processor.image_path = filename
    # print("file:", processor.image_path)
    lb_path.config(text=processor.image_path)
    processor.process(processor.image_path)
    processor.showOriginImage()
    

def show_value():
    print("===============>", processor.lower_blue, processor.upper_blue)
    processor.process(processor.image_path)
    processor.showResult()

ttk.Button(root,text='Open Files',command=select_files).place(x=400, y=260)
ttk.Button(root,text='Show Result',command=show_value).place(x=300, y=260)

# HH
vh1 = tk.Variable(root, processor.lower_blue[0])
vh2 = tk.Variable(root, processor.upper_blue[0])

# SS
vs1 = tk.Variable(root, processor.lower_blue[1])
vs2 = tk.Variable(root, processor.upper_blue[1])

# VV
vv1 = tk.Variable(root, processor.lower_blue[2])
vv2 = tk.Variable(root, processor.upper_blue[2])

# H widget
ttk.Label(text='H').place(x=10, y=30)
tk.Scale(root, label='', from_=0, to=255, length=200, resolution=1, orient=tk.HORIZONTAL, variable=vh1, command=call_h1).place(x=50,y=10)
tk.Scale(root, label='', from_=0, to=255, length=200, resolution=1, orient=tk.HORIZONTAL, variable=vh2, command=call_h2).place(x=260,y=10)

# S widget
ttk.Label(text='S').place(x=10, y=80)
tk.Scale(root, label='', from_=0, to=255, length=200, resolution=1, orient=tk.HORIZONTAL, variable=vs1, command=call_s1).place(x=50,y=60)
tk.Scale(root, label='', from_=0, to=255, length=200, resolution=1, orient=tk.HORIZONTAL, variable=vs2, command=call_s2).place(x=260,y=60)

# V widget
ttk.Label(text='V').place(x=10, y=130)
tk.Scale(root, label='', from_=0, to=255, length=200, resolution=1, orient=tk.HORIZONTAL, variable=vv1, command=call_v1).place(x=50,y=110)
tk.Scale(root, label='', from_=0, to=255, length=200, resolution=1, orient=tk.HORIZONTAL, variable=vv2, command=call_v2).place(x=260,y=110)

lb_path = ttk.Label(text='文件路径')
lb_path.place(x=10, y=180)

root.mainloop()